﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="GoogleCodeWiki.cs" company="WikiDocLib team">
//
// nant-cross-compile, an extension for NAnt for cross-compiling C/C++ applications.
// Copyright (C) 2012 nant-cross-compile team
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see http://www.gnu.org/licenses/. 
// </copyright>
// <history>
//   <historyitem date="2012-10-27" change="Created file"/>
// </history>
// 
// --------------------------------------------------------------------------------------------------------------------
namespace WikiDocLib
{
  using System;
  using System.IO;
  using System.Text;

  /// <summary>
  /// Creates google wiki syntax elements.
  /// </summary>
  public class GoogleCodeWiki
  {
    /// <summary>
    /// The string builder which contains the wiki code.
    /// </summary>
    private StringBuilder sb = new StringBuilder();
    
    /// <summary>
    /// Adds a heading.
    /// </summary>
    /// <param name="heading">The heading.</param>
    /// <param name="level">The level.</param>
    public void AddHeading(string heading, int level)
    {
      string value = string.Empty;
      for (int i = 0; i < level; i++)
      {
        value += "=";
      }

      value += " ";
      value += heading;
      value += " ";
      for (int i = 0; i < level; i++)
      {
        value += "=";
      }

      this.sb.AppendLine(value);
    }

    /// <summary>
    /// Adds a table header.
    /// </summary>
    /// <param name="elements">The cell elements.</param>    
    public void AddTableHeader(params string[] elements)
    {
      string tableHeader = "|";
      foreach (string element in elements)
      {
        tableHeader += string.Format("| *{0}* |", element);
      }

      tableHeader += "|";

      this.sb.AppendLine(tableHeader);
    }

    /// <summary>
    /// Adds a table row.
    /// </summary>
    /// <param name="elements">The cell elements.</param>
    public void AddTableRow(params string[] elements)
    {
      string tableRow = "|";
      foreach (string element in elements)
      {
        tableRow += string.Format("| {0} |", ConvertTextToWikiText(element));
      }

      tableRow += "|";
      this.sb.AppendLine(tableRow);
    }

    /// <summary>
    /// Adds a summary tag.
    /// </summary>
    /// <param name="summary">The summary text.</param>
    public void AddSummary(string summary)
    {
      this.sb.AppendLine(string.Format("#summary {0}", summary));
    }

    /// <summary>
    /// Adds a paragraph.
    /// </summary>    
    public void AddParagraph()
    {
      this.sb.AppendLine();
    }

    /// <summary>
    /// Adds the text.
    /// </summary>
    /// <param name="text">The text.</param>
    public void AddText(string text)
    {
      this.sb.Append(text);
    }

    /// <summary>
    /// Adds the bullet list item.
    /// </summary>
    /// <param name="item">The item.</param>
    public void AddBulletListItem(string item)
    {
      this.sb.Append("  * " + item);
    }

    /// <summary>
    /// Writes the wiki data to a file.
    /// </summary>
    /// <param name="filename">The filename.</param>
    public void WriteToFile(string filename)
    {
      StreamWriter sw = new StreamWriter(filename);
      sw.Write(this.sb);
      sw.Close();
    }

    /// <summary>
    /// Returns a <see cref="System.String" /> that represents this instance.
    /// </summary>
    /// <returns>
    /// A <see cref="System.String" /> that represents this instance.
    /// </returns>
    public override string ToString()
    {
      return this.sb.ToString();
    }

    /// <summary>
    /// Converts the text to wiki text.
    /// </summary>
    /// <param name="text">The text.</param>
    /// <returns>The wiki text representation.</returns>
    private static string ConvertTextToWikiText(string text)
    {
      return text.Trim().Replace(Environment.NewLine, "<br>");
    }
  }
}
